#!/bin/bash

SHELL_DIR="$(cd $(dirname $0); pwd)"
PROJECT_DIR=$1
SYSTEM_DIR=$2
system_folder_name=`ls *system* -d`
if [[ ! -d ${SYSTEM_DIR} ]]; then
	echo "无SYSTEM目录，爪巴！"
	exit 1
fi
#echo "当前项目目工作录 :$PROJECT_DIR" 
#echo "插件目录 : $SHELL_DIR"
#echo "当前项目刷机包的system目录 :$SYSTEM_DIR"
#echo -e "\e[36m      >>>>>>>>>>     .     >>>>>>>>>> \e[0m"
echo ""
echo ""

if [[ -f $SHELL_DIR/Insides/apktool/apktool.jar ]]; then
	ApkTool="java -jar $SHELL_DIR/Insides/apktool/apktool.jar" 
	echo "ApkTool version:" && $ApkTool -version
	
else
	echo -e "\e[36m     >>>>>>>>>   没有找到apktool   >>>>>>>> \e[0m"
	echo "   ----->请在$SHELL_DIR/Insides/apktool目录下查看是否有apktool "
	sleep 10
	exit 1
fi

lin_dir=${PROJECT_DIR##*/}
if [ ! -d $SHELL_DIR/$lin_dir  ];then
  mkdir $SHELL_DIR/$lin_dir
fi

if [[ -f $SYSTEM_DIR/app/MIUIThemeManager/MIUIThemeManager.apk ]]; then
	echo -e "\e[36m      >>>>>>>>>>>   开始尝试主题破解     >>>>>>>>>>> \e[0m"
	echo ""
	echo "" 
	cp $SYSTEM_DIR/app/MIUIThemeManager/MIUIThemeManager.apk $SHELL_DIR/$lin_dir/MIUIThemeManager.apk.bak
		
	$ApkTool d -q -r -f -o $SHELL_DIR/MIUIThemeManager $SYSTEM_DIR/app/MIUIThemeManager/MIUIThemeManager.apk		

	if [ -d $SHELL_DIR/MIUIThemeManager ]; then
		echo -e "\e[36m    ------------->正在主题破解    \e[0m"
		echo ""
		echo ""
		Crack_File2=$(find $SHELL_DIR/MIUIThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl 'DRM_ERROR_UNKNOWN' | sed 's/^\.\///' | sort)
		sed -i 's/DRM_ERROR_UNKNOWN/DRM_SUCCESS/g' $Crack_File2
		Crack_File3=$(find $SHELL_DIR/MIUIThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl 'OnlineResourceDetail' | grep 'OnlineResourceDetailPresenter' | sed 's/^\.\///' | sort)
		sed -i '/OnlineResourceDetail;->bought:Z/i\const/4 v0, 0x1' $Crack_File3
		sed -i '/OnlineResourceDetail;->bought:Z/i\ return v0' $Crack_File3
		Crack_File4=$(find $SHELL_DIR/MIUIThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl 'AdInfo;->targetType:I' | grep -rl 'AdInfo' | sed 's/^\.\///' | sort)
		sed -i '/AdInfo;->targetType:I/i\const/4 v0, 0x0' $Crack_File4
		sed -i '/AdInfo;->targetType:I/i\ return v0' $Crack_File4
		Crack_File5=$(find $SHELL_DIR/MIUIThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl 'Parcel;->obtain()Landroid/os/Parcel;' | grep -rl 'IAdListener$Stub$Proxy' | sed 's/^\.\///' | sort)
		sed -i '/Parcel;->obtain()Landroid\/os\/Parcel;/i\ return-void' $Crack_File5
		Crack_File6=$(find $SHELL_DIR/MIUIThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl '"using_theme_show_ad"' | grep -rl 'const-string v0, "using_theme_show_ad"' | sed 's/^\.\///' | sort)
		sed -i '/"using_theme_show_ad"/i\const/4 v0, 0x0'  $Crack_File6
		sed -i '/"using_theme_show_ad"/i\ return v0'  $Crack_File6

		$ApkTool b -q -f -o $SYSTEM_DIR/app/MIUIThemeManager/MIUIThemeManager.apk $SHELL_DIR/MIUIThemeManager
		${su} rm -rf $SHELL_DIR/MIUIThemeManager
		echo -e "\e[36m        主题破解成功<-----------------    \e[0m"
		echo -e "\e[32msuccess\e[0m"
		sleep 3
		exit 0
	fi
	echo -e "\e[36m        主题破解失败<-----------------    \e[0m"	
	sleep 3
	exit 1	
					
fi	

echo -e "\e[36m        ----------->找不到MIUI主题APP<--------------    \e[0m"	
echo -e "\e[36m        ----------->找不到MIUI主题APP<--------------    \e[0m"
echo -e "\e[36m        ----------->找不到MIUI主题APP<--------------    \e[0m"
sleep 3
#chmod -R 0777 $PROJECT_DIR
echo -e "\e[32mDone\e[0m"


